﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using MALRECrewrite.Models;
using System.IO;
using System.Net;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media.Imaging;

namespace MALRECrewrite.Etc
{
    public class ImageCacheConverter : IMultiValueConverter
    {
        string filepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"MALRECRewrite\Images\");
        
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                return new BitmapImage(new Uri("http://cdn.myanimelist.net/images/anime/6/25093.jpg"));
            }
            else
            {
                string path = (string)values.Where(x => x.GetType() == typeof(string)).First();
                int aid = (int)values.Where(x => x.GetType() == typeof(int)).First();

                string tempfilepath = filepath + aid + ".jpg";

                BitmapImage bmp = new BitmapImage();
                bmp.BeginInit();

                if (File.Exists(tempfilepath))
                    bmp.UriSource = new Uri(tempfilepath);
                else
                    bmp.UriSource = new Uri(path);

                bmp.EndInit();
                return bmp;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
